package dao;

import common.Group;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.sql.*;
import java.util.Properties;

public class GetGroup {
    public Group getGroup(Group gp) {
        Connection conn = null;
        ResultSet res = null;
        Group group = new Group();
        try {
            boolean f = false;
            Properties info=new Properties();
            info.load(new FileInputStream("src\\sql.properties"));
            String user=info.getProperty("user");
            String password=info.getProperty("password");
            String Driver=info.getProperty("Driver");
            String url=info.getProperty("url");
            //注册驱动
            Class.forName(Driver);
            //获取连接
            conn = DriverManager.getConnection(url, user, password);
            String sql1 = "select * from groupp where group_id = ?";
            String sql2 = "select * from grouplist where group_id = ? and statement = ?";
            String sql3 = "select * from users where userid = ?";
            PreparedStatement presta1 = conn.prepareStatement(sql1);
            presta1.setString(1, gp.getGroupid());
            res = presta1.executeQuery();
            if (res.next()) {
                group.setGroupid(res.getString("group_id"));
                group.setGroupname(res.getString("group_name"));
                group.setGroupheadimage(res.getString("group_photo"));
                group.setGroupsign(res.getString("group_introduction"));
                // System.out.println("当前groupid对应的信息：" + group);
            }
            PreparedStatement presta2 = conn.prepareStatement(sql2);
            presta2.setString(1, gp.getGroupid());
            presta2.setInt(2, 2);
            res = presta2.executeQuery();
            String str=null;
            if (res.next()) {
                str = res.getString("member_id");
                //System.out.println(str + "咪咪");
            }
            PreparedStatement presta3 = conn.prepareStatement(sql3);
            presta3.setString(1, str);
            res = presta3.executeQuery();
            if (res.next()) {
                group.setOwnerid(res.getString("userid"));
                group.setOwnername(res.getString("username"));
                group.setOwnerheadimage(res.getString("headimage"));
                //System.out.println("群主的信息：" + group.getOwnerid());
            }
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return group;
    }
}
